﻿using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DAL.DBManagers
{
    public class FlagsManager : BaseManager<Flag>
    {
        public FlagsManager()
            : base()
        {
        }

        public override IEnumerable<Flag> GetAll()
        {
            return base.GetAll().Where(w => w.IsActive && w.FinishDate == null);
        }

        public List<FlagHelper> GetFlags()
        {
            var context = (PortEntities)this._context;

            return (from f in context.Flags
                    join n in context.NavigationModes on f.NavigationModeID equals n.NavigationModeID
                    where f.IsActive && n.IsActive && f.FinishDate == null && n.FinishDate == null
                    select new FlagHelper
                    {
                        RecordID = f.RecordID,
                        ID = f.FlagID,
                        Name = f.Name,
                        NameGeo = f.NameGeo,
                        BeginDate = f.BeginDate,
                        FinishDate = f.FinishDate,
                        NavigationMode = new NavigationModeHelper
                        {
                            RecordID = n.RecordID,
                            ID = n.NavigationModeID,
                            Name = n.Name,
                            NameGeo = n.NameGeo,
                            BeginDate = n.BeginDate,
                            FinishDate = n.FinishDate,
                            IsActive = n.IsActive
                        },
                        IsActive = f.IsActive
                    }).OrderBy(o => o.Name).ToList();
        }

        public List<FlagHelper> GetFlags(string sortBy)
        {
            var context = (PortEntities)this._context;

            List<FlagHelper> prodList = (from f in context.Flags
                                         join n in context.NavigationModes on f.NavigationModeID equals n.NavigationModeID
                                         where f.IsActive && n.IsActive && f.FinishDate == null && n.FinishDate == null
                                         select new FlagHelper
                                         {
                                             RecordID = f.RecordID,
                                             ID = f.FlagID,
                                             Name = f.Name,
                                             NameGeo = f.NameGeo,
                                             BeginDate = f.BeginDate,
                                             FinishDate = f.FinishDate,
                                             NavigationMode = new NavigationModeHelper
                                             {
                                                 RecordID = n.RecordID,
                                                 ID = n.NavigationModeID,
                                                 Name = n.Name,
                                                 NameGeo = n.NameGeo,
                                                 BeginDate = n.BeginDate,
                                                 FinishDate = n.FinishDate,
                                                 IsActive = n.IsActive
                                             },
                                             IsActive = f.IsActive
                                         }).OrderBy(o => o.Name).ToList();

            if (sortBy != "")
            {
                DAL.Models.GenericComparer<FlagHelper> cmp = new DAL.Models.GenericComparer<FlagHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public List<FlagHelper> GetFlags(Guid FlagID)
        {
            var context = (PortEntities)this._context;

            return (from f in context.Flags
                    join n in context.NavigationModes on f.NavigationModeID equals n.NavigationModeID
                    where f.IsActive && n.IsActive && f.FlagID == FlagID &&
                          n.RecordID == context.NavigationModes.Where(w => w.IsActive && w.NavigationModeID == n.NavigationModeID && w.BeginDate <= f.BeginDate)
                                               .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                    select new FlagHelper
                    {
                        RecordID = f.RecordID,
                        ID = f.FlagID,
                        Name = f.Name,
                        NameGeo = f.NameGeo,
                        BeginDate = f.BeginDate,
                        FinishDate = f.FinishDate,
                        NavigationMode = new NavigationModeHelper
                        {
                            RecordID = n.RecordID,
                            ID = n.NavigationModeID,
                            Name = n.Name,
                            NameGeo = n.NameGeo,
                            BeginDate = n.BeginDate,
                            FinishDate = n.FinishDate,
                            IsActive = n.IsActive
                        },
                        IsActive = f.IsActive
                    }).OrderByDescending(o => o.BeginDate).ToList();
        }

        public List<FlagHelper> GetFlags(Guid FlagID, string sortBy)
        {
            var context = (PortEntities)this._context;

            var prodList = (from f in context.Flags
                            join n in context.NavigationModes on f.NavigationModeID equals n.NavigationModeID
                            where f.IsActive && n.IsActive && f.FlagID == FlagID &&
                                  n.RecordID == context.NavigationModes.Where(w => w.IsActive && w.NavigationModeID == n.NavigationModeID && w.BeginDate <= f.BeginDate)
                                                       .OrderByDescending(o => o.BeginDate).FirstOrDefault().RecordID
                            select new FlagHelper
                            {
                                RecordID = f.RecordID,
                                ID = f.FlagID,
                                Name = f.Name,
                                NameGeo = f.NameGeo,
                                BeginDate = f.BeginDate,
                                FinishDate = f.FinishDate,
                                NavigationMode = new NavigationModeHelper
                                {
                                    RecordID = n.RecordID,
                                    ID = n.NavigationModeID,
                                    Name = n.Name,
                                    NameGeo = n.NameGeo,
                                    BeginDate = n.BeginDate,
                                    FinishDate = n.FinishDate,
                                    IsActive = n.IsActive
                                },
                                IsActive = f.IsActive
                            }).OrderByDescending(o => o.BeginDate).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<FlagHelper> cmp = new DAL.Models.GenericComparer<FlagHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }



        public void RepiarDates(Guid FlagID)
        {
            var flags = this.Find(f => f.IsActive && f.FlagID == FlagID).OrderBy(o => o.BeginDate).ToList();

            var count = flags.Count();

            for (int i = 0; i < count; i++)
            {
                if (i < count - 1)
                {
                    if (flags[i].FinishDate != flags[i + 1].BeginDate)
                    {
                        flags[i].FinishDate = flags[i + 1].BeginDate;
                    }
                }
                else if (i == count - 1)
                {
                    if (flags[i].FinishDate != null)
                    {
                        flags[i].FinishDate = null;
                    }
                }
            }

            SaveChanges();
        }
    }
}
